﻿using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
using NetTao.Model;

namespace NetTao.Dao
{
    public class DaoChapter : DaoBase
    {
        public DaoChapter(string connectionString)
            : base(connectionString)
        {
            ConnectionString = connectionString;
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(ChapterInfo model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into jieqi_article_chapter(");
            strSql.Append("siteid,articleid,articlename,volumeid,posterid,poster,postdate,lastupdate,chaptername,chapterorder,size,saleprice,salenum,totalcost,attachment,isvip,chaptertype,power,display,chapterid,chapterlink,webflag)");
            strSql.Append(" values (");
            strSql.Append("?siteid,?articleid,?articlename,?volumeid,?posterid,?poster,?postdate,?lastupdate,?chaptername,?chapterorder,?size,?saleprice,?salenum,?totalcost,?attachment,?isvip,?chaptertype,?power,?display,?chapterid,?chapterlink,?webflag)");
            MySqlParameter[] parameters =
                {
                    new MySqlParameter("?siteid", MySqlDbType.Int16, 6),
                    new MySqlParameter("?articleid", MySqlDbType.Int32, 11),
                    new MySqlParameter("?articlename", MySqlDbType.VarChar, 50),
                    new MySqlParameter("?volumeid", MySqlDbType.Int32, 11),
                    new MySqlParameter("?posterid", MySqlDbType.Int32, 11),
                    new MySqlParameter("?poster", MySqlDbType.VarChar, 30),
                    new MySqlParameter("?postdate", MySqlDbType.Int32, 11),
                    new MySqlParameter("?lastupdate", MySqlDbType.Int32, 11),
                    new MySqlParameter("?chaptername", MySqlDbType.VarChar, 100),
                    new MySqlParameter("?chapterorder", MySqlDbType.Int16, 6),
                    new MySqlParameter("?size", MySqlDbType.Int32, 11),
                    new MySqlParameter("?saleprice", MySqlDbType.Int32, 11),
                    new MySqlParameter("?salenum", MySqlDbType.Int32, 11),
                    new MySqlParameter("?totalcost", MySqlDbType.Int32, 11),
                    new MySqlParameter("?attachment", MySqlDbType.Text),
                    new MySqlParameter("?isvip", MySqlDbType.Int16, 1),
                    new MySqlParameter("?chaptertype", MySqlDbType.Int16, 1),
                    new MySqlParameter("?power", MySqlDbType.Int16, 1),
                    new MySqlParameter("?display", MySqlDbType.Int16, 1),
                    new MySqlParameter("?chapterid", MySqlDbType.Int32, 11),
                    new MySqlParameter("?chapterlink", MySqlDbType.String),
                    new MySqlParameter("?webflag", MySqlDbType.String)
                };
            parameters[0].Value = model.siteid;
            parameters[1].Value = model.articleid;
            parameters[2].Value = model.articlename;
            parameters[3].Value = model.volumeid;
            parameters[4].Value = model.posterid;
            parameters[5].Value = model.poster;
            parameters[6].Value = model.postdate;
            parameters[7].Value = model.lastupdate;
            parameters[8].Value = model.chaptername;
            parameters[9].Value = model.chapterorder;
            parameters[10].Value = model.size;
            parameters[11].Value = model.saleprice;
            parameters[12].Value = model.salenum;
            parameters[13].Value = model.totalcost;
            parameters[14].Value = model.attachment;
            parameters[15].Value = model.isvip;
            parameters[16].Value = model.chaptertype;
            parameters[17].Value = model.power;
            parameters[18].Value = model.display;
            parameters[19].Value = model.chapterid;
            parameters[20].Value = model.contentslink;
            parameters[21].Value = model.webflag;
            return MySqlHelper.ExecuteNonQuery(Connection, System.Data.CommandType.Text, strSql.ToString(), parameters) >= 0;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(NetTao.Model.ChapterInfo model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update jieqi_article_chapter set ");
            strSql.Append("siteid=?siteid,");
            strSql.Append("articleid=?articleid,");
            strSql.Append("articlename=?articlename,");
            strSql.Append("volumeid=?volumeid,");
            strSql.Append("posterid=?posterid,");
            strSql.Append("poster=?poster,");
            strSql.Append("postdate=?postdate,");
            strSql.Append("lastupdate=?lastupdate,");
            strSql.Append("chaptername=?chaptername,");
            strSql.Append("chapterorder=?chapterorder,");
            strSql.Append("size=?size,");
            strSql.Append("saleprice=?saleprice,");
            strSql.Append("salenum=?salenum,");
            strSql.Append("totalcost=?totalcost,");
            strSql.Append("attachment=?attachment,");
            strSql.Append("isvip=?isvip,");
            strSql.Append("chaptertype=?chaptertype,");
            strSql.Append("power=?power,");
            strSql.Append("display=?display");
            strSql.Append(" where chapterid=?chapterid");
            MySqlParameter[] parameters = {
					new MySqlParameter("?siteid", MySqlDbType.Int16,6),
					new MySqlParameter("?articleid", MySqlDbType.Int32,11),
					new MySqlParameter("?articlename", MySqlDbType.VarChar,50),
					new MySqlParameter("?volumeid", MySqlDbType.Int32,11),
					new MySqlParameter("?posterid", MySqlDbType.Int32,11),
					new MySqlParameter("?poster", MySqlDbType.VarChar,30),
					new MySqlParameter("?postdate", MySqlDbType.Int32,11),
					new MySqlParameter("?lastupdate", MySqlDbType.Int32,11),
					new MySqlParameter("?chaptername", MySqlDbType.VarChar,100),
					new MySqlParameter("?chapterorder", MySqlDbType.Int16,6),
					new MySqlParameter("?size", MySqlDbType.Int32,11),
					new MySqlParameter("?saleprice", MySqlDbType.Int32,11),
					new MySqlParameter("?salenum", MySqlDbType.Int32,11),
					new MySqlParameter("?totalcost", MySqlDbType.Int32,11),
					new MySqlParameter("?attachment", MySqlDbType.Text),
					new MySqlParameter("?isvip", MySqlDbType.Int16,1),
					new MySqlParameter("?chaptertype", MySqlDbType.Int16,1),
					new MySqlParameter("?power", MySqlDbType.Int16,1),
					new MySqlParameter("?display", MySqlDbType.Int16,1),
					new MySqlParameter("?chapterid", MySqlDbType.Int32,11)};
            parameters[0].Value = model.siteid;
            parameters[1].Value = model.articleid;
            parameters[2].Value = model.articlename;
            parameters[3].Value = model.volumeid;
            parameters[4].Value = model.posterid;
            parameters[5].Value = model.poster;
            parameters[6].Value = model.postdate;
            parameters[7].Value = model.lastupdate;
            parameters[8].Value = model.chaptername;
            parameters[9].Value = model.chapterorder;
            parameters[10].Value = model.size;
            parameters[11].Value = model.saleprice;
            parameters[12].Value = model.salenum;
            parameters[13].Value = model.totalcost;
            parameters[14].Value = model.attachment;
            parameters[15].Value = model.isvip;
            parameters[16].Value = model.chaptertype;
            parameters[17].Value = model.power;
            parameters[18].Value = model.display;
            parameters[19].Value = model.chapterid;

            return MySqlHelper.ExecuteNonQuery(Connection, System.Data.CommandType.Text, strSql.ToString(), parameters) >= 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int chapterid)
        {

            var strSql = new StringBuilder();
            strSql.Append("delete from jieqi_article_chapter ");
            strSql.Append(" where chapterid=?chapterid");
            MySqlParameter[] parameters = {
					new MySqlParameter("?chapterid", MySqlDbType.Int32)
			};
            parameters[0].Value = chapterid;
            return MySqlHelper.ExecuteNonQuery(Connection, System.Data.CommandType.Text, strSql.ToString(), parameters) >= 0;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ChapterInfo Get(int chapterid)
        {

            var strSql = new StringBuilder();
            strSql.Append("select chapterid,siteid,articleid,articlename,volumeid,posterid,poster,postdate,lastupdate,chaptername,chapterorder,size,saleprice,salenum,totalcost,attachment,isvip,chaptertype,power,display from jieqi_article_chapter ");
            strSql.Append(" where chapterid=?chapterid");
            MySqlParameter[] parameters = {
					new MySqlParameter("?chapterid", MySqlDbType.Int32)
			};
            parameters[0].Value = chapterid;

            var model = new ChapterInfo();
            using (MySqlDataReader dr = MySqlHelper.ExecuteReader(Connection, System.Data.CommandType.Text, strSql.ToString(), parameters))
            {
                while (dr.Read())
                {
                    #region
                    if (dr["chapterid"] != null && dr["chapterid"].ToString() != "")
                    {
                        model.chapterid = int.Parse(dr["chapterid"].ToString());
                    }
                    if (dr["siteid"] != null && dr["siteid"].ToString() != "")
                    {
                        model.siteid = int.Parse(dr["siteid"].ToString());
                    }
                    if (dr["articleid"] != null && dr["articleid"].ToString() != "")
                    {
                        model.articleid = int.Parse(dr["articleid"].ToString());
                    }
                    if (dr["articlename"] != null)
                    {
                        model.articlename = dr["articlename"].ToString();
                    }
                    if (dr["volumeid"] != null && dr["volumeid"].ToString() != "")
                    {
                        model.volumeid = int.Parse(dr["volumeid"].ToString());
                    }
                    if (dr["posterid"] != null && dr["posterid"].ToString() != "")
                    {
                        model.posterid = int.Parse(dr["posterid"].ToString());
                    }
                    if (dr["poster"] != null)
                    {
                        model.poster = dr["poster"].ToString();
                    }
                    if (dr["postdate"] != null && dr["postdate"].ToString() != "")
                    {
                        model.postdate = int.Parse(dr["postdate"].ToString());
                    }
                    if (dr["lastupdate"] != null && dr["lastupdate"].ToString() != "")
                    {
                        model.lastupdate = int.Parse(dr["lastupdate"].ToString());
                    }
                    if (dr["chaptername"] != null)
                    {
                        model.chaptername = dr["chaptername"].ToString();
                    }
                    if (dr["chapterorder"] != null && dr["chapterorder"].ToString() != "")
                    {
                        model.chapterorder = int.Parse(dr["chapterorder"].ToString());
                    }
                    if (dr["size"] != null && dr["size"].ToString() != "")
                    {
                        model.size = int.Parse(dr["size"].ToString());
                    }
                    if (dr["saleprice"] != null && dr["saleprice"].ToString() != "")
                    {
                        model.saleprice = int.Parse(dr["saleprice"].ToString());
                    }
                    if (dr["salenum"] != null && dr["salenum"].ToString() != "")
                    {
                        model.salenum = int.Parse(dr["salenum"].ToString());
                    }
                    if (dr["totalcost"] != null && dr["totalcost"].ToString() != "")
                    {
                        model.totalcost = int.Parse(dr["totalcost"].ToString());
                    }
                    if (dr["attachment"] != null)
                    {
                        model.attachment = dr["attachment"].ToString();
                    }
                    if (dr["isvip"] != null && dr["isvip"].ToString() != "")
                    {
                        model.isvip = int.Parse(dr["isvip"].ToString());
                    }
                    if (dr["chaptertype"] != null && dr["chaptertype"].ToString() != "")
                    {
                        model.chaptertype = int.Parse(dr["chaptertype"].ToString());
                    }
                    if (dr["power"] != null && dr["power"].ToString() != "")
                    {
                        model.power = int.Parse(dr["power"].ToString());
                    }
                    if (dr["display"] != null && dr["display"].ToString() != "")
                    {
                        model.display = int.Parse(dr["display"].ToString());
                    }
                    #endregion
                }
            }
            return model;
        }

        /// <summary>
        /// 根据书ID获取章节列表
        /// </summary>
        /// <param name="articleid"></param>
        /// <returns></returns>
        public IList<ChapterInfo> GetList(int articleid)
        {
            var strSql = new StringBuilder();
            strSql.Append("select chapterid,siteid,articleid,articlename,volumeid,posterid,poster,postdate,lastupdate,chaptername,chapterorder,size,saleprice,salenum,totalcost,attachment,isvip,chaptertype,power,display from jieqi_article_chapter ");
            strSql.Append(" where articleid=?articleid");
            MySqlParameter[] parameters = {
					new MySqlParameter("?articleid", MySqlDbType.Int32)
			};
            parameters[0].Value = articleid;
            IList<ChapterInfo> list = new List<ChapterInfo>();
            using (MySqlDataReader dr = MySqlHelper.ExecuteReader(Connection, System.Data.CommandType.Text, strSql.ToString(), parameters))
            {
                while (dr.Read())
                {
                    var model = new ChapterInfo();
                    #region
                    if (dr["chapterid"] != null && dr["chapterid"].ToString() != "")
                    {
                        model.chapterid = int.Parse(dr["chapterid"].ToString());
                    }
                    if (dr["siteid"] != null && dr["siteid"].ToString() != "")
                    {
                        model.siteid = int.Parse(dr["siteid"].ToString());
                    }
                    if (dr["articleid"] != null && dr["articleid"].ToString() != "")
                    {
                        model.articleid = int.Parse(dr["articleid"].ToString());
                    }
                    if (dr["articlename"] != null)
                    {
                        model.articlename = dr["articlename"].ToString();
                    }
                    if (dr["volumeid"] != null && dr["volumeid"].ToString() != "")
                    {
                        model.volumeid = int.Parse(dr["volumeid"].ToString());
                    }
                    if (dr["posterid"] != null && dr["posterid"].ToString() != "")
                    {
                        model.posterid = int.Parse(dr["posterid"].ToString());
                    }
                    if (dr["poster"] != null)
                    {
                        model.poster = dr["poster"].ToString();
                    }
                    if (dr["postdate"] != null && dr["postdate"].ToString() != "")
                    {
                        model.postdate = int.Parse(dr["postdate"].ToString());
                    }
                    if (dr["lastupdate"] != null && dr["lastupdate"].ToString() != "")
                    {
                        model.lastupdate = int.Parse(dr["lastupdate"].ToString());
                    }
                    if (dr["chaptername"] != null)
                    {
                        model.chaptername = dr["chaptername"].ToString();
                    }
                    if (dr["chapterorder"] != null && dr["chapterorder"].ToString() != "")
                    {
                        model.chapterorder = int.Parse(dr["chapterorder"].ToString());
                    }
                    if (dr["size"] != null && dr["size"].ToString() != "")
                    {
                        model.size = int.Parse(dr["size"].ToString());
                    }
                    if (dr["saleprice"] != null && dr["saleprice"].ToString() != "")
                    {
                        model.saleprice = int.Parse(dr["saleprice"].ToString());
                    }
                    if (dr["salenum"] != null && dr["salenum"].ToString() != "")
                    {
                        model.salenum = int.Parse(dr["salenum"].ToString());
                    }
                    if (dr["totalcost"] != null && dr["totalcost"].ToString() != "")
                    {
                        model.totalcost = int.Parse(dr["totalcost"].ToString());
                    }
                    if (dr["attachment"] != null)
                    {
                        model.attachment = dr["attachment"].ToString();
                    }
                    if (dr["isvip"] != null && dr["isvip"].ToString() != "")
                    {
                        model.isvip = int.Parse(dr["isvip"].ToString());
                    }
                    if (dr["chaptertype"] != null && dr["chaptertype"].ToString() != "")
                    {
                        model.chaptertype = int.Parse(dr["chaptertype"].ToString());
                    }
                    if (dr["power"] != null && dr["power"].ToString() != "")
                    {
                        model.power = int.Parse(dr["power"].ToString());
                    }
                    if (dr["display"] != null && dr["display"].ToString() != "")
                    {
                        model.display = int.Parse(dr["display"].ToString());
                    }
                    #endregion
                    list.Add(model);
                }
            }
            return list;
        }

        public IList<ChapterInfo> GetList(int articleid,string webflag)
        {
            var strSql = new StringBuilder();
            strSql.Append("select chapterid,siteid,articleid,articlename,volumeid,posterid,poster,postdate,lastupdate,chaptername,chapterorder,size,saleprice,salenum,totalcost,attachment,isvip,chaptertype,power,display from jieqi_article_chapter ");
            strSql.Append(" where articleid=?articleid and webflag=?webflag");
            MySqlParameter[] parameters = {
					new MySqlParameter("?articleid", MySqlDbType.Int32),
                    new MySqlParameter("?webflag", MySqlDbType.String)
			};
            parameters[0].Value = articleid;
            parameters[1].Value = webflag;
            IList<ChapterInfo> list = new List<ChapterInfo>();
            using (MySqlDataReader dr = MySqlHelper.ExecuteReader(Connection, System.Data.CommandType.Text, strSql.ToString(), parameters))
            {
                while (dr.Read())
                {
                    var model = new ChapterInfo();
                    #region
                    if (dr["chapterid"] != null && dr["chapterid"].ToString() != "")
                    {
                        model.chapterid = int.Parse(dr["chapterid"].ToString());
                    }
                    if (dr["siteid"] != null && dr["siteid"].ToString() != "")
                    {
                        model.siteid = int.Parse(dr["siteid"].ToString());
                    }
                    if (dr["articleid"] != null && dr["articleid"].ToString() != "")
                    {
                        model.articleid = int.Parse(dr["articleid"].ToString());
                    }
                    if (dr["articlename"] != null)
                    {
                        model.articlename = dr["articlename"].ToString();
                    }
                    if (dr["volumeid"] != null && dr["volumeid"].ToString() != "")
                    {
                        model.volumeid = int.Parse(dr["volumeid"].ToString());
                    }
                    if (dr["posterid"] != null && dr["posterid"].ToString() != "")
                    {
                        model.posterid = int.Parse(dr["posterid"].ToString());
                    }
                    if (dr["poster"] != null)
                    {
                        model.poster = dr["poster"].ToString();
                    }
                    if (dr["postdate"] != null && dr["postdate"].ToString() != "")
                    {
                        model.postdate = int.Parse(dr["postdate"].ToString());
                    }
                    if (dr["lastupdate"] != null && dr["lastupdate"].ToString() != "")
                    {
                        model.lastupdate = int.Parse(dr["lastupdate"].ToString());
                    }
                    if (dr["chaptername"] != null)
                    {
                        model.chaptername = dr["chaptername"].ToString();
                    }
                    if (dr["chapterorder"] != null && dr["chapterorder"].ToString() != "")
                    {
                        model.chapterorder = int.Parse(dr["chapterorder"].ToString());
                    }
                    if (dr["size"] != null && dr["size"].ToString() != "")
                    {
                        model.size = int.Parse(dr["size"].ToString());
                    }
                    if (dr["saleprice"] != null && dr["saleprice"].ToString() != "")
                    {
                        model.saleprice = int.Parse(dr["saleprice"].ToString());
                    }
                    if (dr["salenum"] != null && dr["salenum"].ToString() != "")
                    {
                        model.salenum = int.Parse(dr["salenum"].ToString());
                    }
                    if (dr["totalcost"] != null && dr["totalcost"].ToString() != "")
                    {
                        model.totalcost = int.Parse(dr["totalcost"].ToString());
                    }
                    if (dr["attachment"] != null)
                    {
                        model.attachment = dr["attachment"].ToString();
                    }
                    if (dr["isvip"] != null && dr["isvip"].ToString() != "")
                    {
                        model.isvip = int.Parse(dr["isvip"].ToString());
                    }
                    if (dr["chaptertype"] != null && dr["chaptertype"].ToString() != "")
                    {
                        model.chaptertype = int.Parse(dr["chaptertype"].ToString());
                    }
                    if (dr["power"] != null && dr["power"].ToString() != "")
                    {
                        model.power = int.Parse(dr["power"].ToString());
                    }
                    if (dr["display"] != null && dr["display"].ToString() != "")
                    {
                        model.display = int.Parse(dr["display"].ToString());
                    }
                    #endregion
                    list.Add(model);
                }
            }
            return list;
        }



        /// <summary>
        /// 获取最大CHAPTER ID
        /// </summary>
        /// <returns></returns>
        public Int32 GetChapterMaxID()
        {
            var strSql = new StringBuilder();
            strSql.Append("select max(chapterid) as  articleid from jieqi_article_chapter");
            using (MySqlDataReader dr = MySqlHelper.ExecuteReader(Connection, System.Data.CommandType.Text, strSql.ToString(), null))
            {
                while (dr.Read())
                {
                    if (!string.IsNullOrEmpty(dr[0].ToString()))
                    {
                        return Int32.Parse(dr[0].ToString());
                    }
                }
            }
            return 0;
        }
    }
}
